package sub

import (
	"encoding/json"
	"log"
	l "order_srv/basic/init"
	"order_srv/handler/models"
)

func Consum() {
	order := models.Orders{}
	rabbitmq := l.NewRabbitMQSimple("" +
		"2301a")
	simple, err := rabbitmq.ConsumeSimple()
	if err != nil {
		return
	}

	forever := make(chan bool)
	go func() {
		for d := range simple {
			//消息逻辑处理，可以自行设计逻辑
			log.Printf("Received a message: %s", d.Body)
			json.Unmarshal(d.Body, &order)
			err = order.CreateOrders()
			if err != nil {
				log.Printf("处理消息失败: %v", err)
				d.Nack(false, false) // 拒绝消息，且不重新入队（根据业务需求调整）
				continue
			}
			d.Ack(false) // 关键：通知Broker删除该消息
		}
	}()
	log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
	<-forever
	//var order models.Order
	//rabbitmq := l.NewRabbitMQSimple("" +
	//	"2301a")
	//simple, err := rabbitmq.ConsumeSimple()
	//if err != nil {
	//	return
	//}
	//
	//forever := make(chan bool)
	////启用协程处理消息
	//go func() {
	//	for d := range simple {
	//		//消息逻辑处理，可以自行设计逻辑
	//		log.Printf("Received a message: %s", d.Body)
	//		err = json.Unmarshal(d.Body, &order)
	//		if err != nil {
	//			return
	//		}
	//		err = order.OrderAdd()
	//		if err != nil {
	//			return
	//		}
	//	}
	//}()
	//
	//log.Printf(" [*] Waiting for messages. To exit press CTRL+C")
	//<-forever

}
